package com.zl.data;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.zl.data.manger.DataManger;

/**
 * @author ZL
 */
public class MainActivity extends AppCompatActivity {
    private TextView tvMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvMsg = findViewById(R.id.tv_msg);
        findViewById(R.id.btn_s_01).setOnClickListener(v -> {
            String key = "app_info_msg";
            String value = "I Love IDE";
            saveMsg(v.getContext(), key, value);
        });
        findViewById(R.id.btn_s_02).setOnClickListener(v -> {
            String key = "app_info_msg";
            String value = "I Love IDE 002";
            saveMsg(v.getContext(), key, value);
        });

        findViewById(R.id.btn_r_01).setOnClickListener(v -> {
            String key = "app_info_msg";
            readMsg(v.getContext(), key);
        });
        findViewById(R.id.btn_r_02).setOnClickListener(v -> {
            String key = "app_info_msg";
            readMsg(v.getContext(), key);
        });
//        DataConstant.setDatabasePath(AppConstant.DATA_PATH);
//        DataUtil.getInstance(this);
    }


    private void saveMsg(Context mContext, String key, String value) {
        long start_time = System.currentTimeMillis();
//        DataUtil.getInstance(this).replace(key, value.getBytes());
//        DataServiceManger.encode(key, value);
        DataManger.encode(key, value);
        System.out.println("存储数据时间：" + (System.currentTimeMillis() - start_time));
    }

    private void readMsg(Context mContext, String key) {
        long start_time = System.currentTimeMillis();
//        byte[] value = DataUtil.getInstance(this).query(key);
//        String msg = new String(value);
        String msg = DataManger.decodeString(key);
        System.out.println("读取数据时间：" + (System.currentTimeMillis() - start_time));
        showMsg("key:" + msg);
    }

    private void showMsg(String msg) {
        if (null == tvMsg || TextUtils.isEmpty(msg)) {
            return;
        }
        String last = tvMsg.getText().toString();
        msg = last + "\n" + msg;
        final String finalMsg = msg;
        tvMsg.post(new Runnable() {
            @Override
            public void run() {
                tvMsg.setText(finalMsg);
            }
        });
    }
}